package com.distribute.portal.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.text.StrBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.distribute.common.pojo.DistributeResult;
import com.distribute.common.util.HttpClientUtil;
import com.distribute.pojo.DbUser;
import com.distribute.portal.service.UserService;

/**
 * 用户管理Service
 * <p>
 * Title: UserServiceImpl
 * </p>
 * <p>
 * Description:
 * </p>
 * 
 * @version 1.0
 */
@Service
public class UserServiceImpl implements UserService {
	@Autowired

	@Value("${SSO_BASE_URL}")
	public String SSO_BASE_URL;
	@Value("${SSO_DOMAIN_BASE_USRL}")
	public String SSO_DOMAIN_BASE_USRL;
	@Value("${SSO_USER_TOKEN}")
	private String SSO_USER_TOKEN;
	@Value("${SSO_PAGE_LOGIN}")
	public String SSO_PAGE_LOGIN;
	@Value("${SSO_USER_CHECK_PASS}")
	public String SSO_USER_CHECK_PASS;
	@Value("${SSO_USER_CHANGE_INFO}")
	public String SSO_USER_CHANGE_INFO;
	@Value("${SSO_USER_CHANGE_PASS}")
	public String SSO_USER_CHANGE_PASS;

	@Override
	public DbUser getUserByToken(String token) {
		try {
			// 调用sso系统的服务，根据token取用户信息
			String json = HttpClientUtil.doGet(SSO_BASE_URL + SSO_USER_TOKEN + token);
			// 把json转换成DistributeResult
			System.out.println(json+"ggggggggg");
			DistributeResult result = DistributeResult.formatToPojo(json, DbUser.class);
			if (result.getStatus() == 200) {
				DbUser user = (DbUser) result.getData();
				return user;
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public DistributeResult  checkUserPass(String userName, String password) {
		Map<String, String> map = new HashMap<>();
		map.put("userName", userName);
		map.put("password", password);
		// 调用sso系统的服务
		String json = HttpClientUtil.doPost(SSO_BASE_URL + SSO_USER_CHECK_PASS, map);
		// 把json转换成DistributeResult
		DistributeResult result = DistributeResult.format(json);
		return result;

	}

	public DistributeResult updateUserInfo(String userName, String phone, String email,String token) {
		Map<String, String> map = new HashMap<>();
		map.put("userName", userName);
		map.put("phone", phone);
		map.put("email", email);
		map.put("token", token);
		// 调用sso系统的服务
		String json = HttpClientUtil.doPost(SSO_BASE_URL + SSO_USER_CHANGE_INFO, map);
		// 把json转换成DistributeResult
		DistributeResult result = DistributeResult.format(json);
		return result;

	}

	public DistributeResult updateUserPass(String userName, String password,String token) {
		Map<String, String> map = new HashMap<>();
		map.put("userName", userName);
		map.put("password", password);
		map.put("token", token);
		// 调用sso系统的服务
		String json = HttpClientUtil.doPost(SSO_BASE_URL + SSO_USER_CHANGE_PASS, map);
		// 把json转换成DistributeResult
		DistributeResult result = DistributeResult.format(json);
		return result;

	}


}
